package com.itsm.sheet.controller;


import com.itsm.common.core.domain.R;
import com.itsm.common.sheet.JzsyEventSheetDto;
import com.itsm.common.sheet.TransferSheetDto;
import com.itsm.sheet.entity.JzsyEventSheet;
import com.itsm.sheet.service.IJzsyEventSheetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(tags = "事件单")
@RestController
@RequestMapping("/itsm/eventSheet")
public class JzsyEventSheetController {

    @Autowired
    private IJzsyEventSheetService jzsyEventSheetService;


    /**
     * 新建事件单
     * */
    @PostMapping("createSheet")
    public R<?> createSheet(@RequestBody JzsyEventSheetDto jzsyEventSheetDto){
        JzsyEventSheet jzsyEventSheet = jzsyEventSheetService.createSheet(jzsyEventSheetDto);
        return  jzsyEventSheet!=null?  R.ok(jzsyEventSheet,"创建成功") : R.fail("创建失败！");
    }


    @PostMapping("getSheetByUser")
    public R<?> getSheetByUser(@RequestParam(value="userId")Long userId){
        List<JzsyEventSheet> result = jzsyEventSheetService.getSheetByUser(userId);
        return R.ok(result);
    }




    /**
     * 关闭单
     * */
    @PostMapping("closeSheet")
    public R<?> closeSheet(@RequestBody TransferSheetDto transferSheetDto){
        JzsyEventSheet jzsyEventSheet = jzsyEventSheetService.closeSheet(transferSheetDto);
        return  jzsyEventSheet!=null?  R.ok(jzsyEventSheet,"关闭成功") : R.fail("关闭失败！");

    }


    @PostMapping("genSheetCode")
    public String genSheetCode(){

        String code = "ES";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Random random = new Random();
        String l1 = random.nextInt(10)+"";
        String l2 = random.nextInt(10)+"";
        String l3 = random.nextInt(10)+"";
        String l4 = random.nextInt(10)+"";

        return code+sdf.format(new Date())+l1+l2+l3+l4;
    }




}
